<?php

/*
 * Copyright (C) 2014 koodo@qq.com.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */

/**
 * 商品主类
 */
class Product extends Model {

    /**
     * 
     * @param type $productId
     */
    public function modifyProduct($dataS) {
        $id = intval($dataS['product_id']);
        if ($id == 0) {
            // 新建商品
            $id = 'NULL';
        }

        if ($id == 'NULL') {
            $SQL = sprintf("INSERT INTO `products_info` (`product_id`,`product_name`,`product_subtitle`,`product_desc`) VALUES (%s,'%s','%s','%s')"
                    , $id
                    , $dataS['product_name']
                    , $dataS['product_subtitle']
                    , $dataS['product_desc']);
        } else {
            $SQL = sprintf("UPDATE `products_info` SET `product_name` = '%s',`product_subtitle` = '%s',`product_desc` = '%s',`product_cat` = '%s' WHERE `product_id` = %s;"
                    , $dataS['product_name']
                    , $dataS['product_subtitle']
                    , $dataS['product_desc']
                    , $dataS['product_cat']
                    , $id);
        }

        if ($id == 'NULL') {
            // 获取新id
            $id = $this->Db->query($SQL);
        } else {
            $this->Db->query($SQL);
        }

        $SQL1 = sprintf("REPLACE INTO `product_onsale` (`product_id`,`discount`,`sale_prices`) VALUES (%s,%s,%s)"
                , $id
                , $dataS['product_discount']
                , $dataS['product_prices']);

        $this->Db->query($SQL1);

        // 商品图片
        if (isset($dataS['product_images'])) {
            $SQL2 = "DELETE FROM `product_images` WHERE `product_id` = $id;";
            $SQL3 = "INSERT INTO `product_images` (`product_id`,`image_path`,`image_type`) VALUES";
            foreach ($dataS['product_images'] as $image) {
                if (preg_match('/product_hpic2__/is', $image)) {
                    $image = str_replace('product_hpic2__', '', $image);
                    rename(dirname(__FILE__) . '/../static/product_hpic2/' . $image, dirname(__FILE__) . '/../static/product_hpic/' . $image);
                }
                $SQL3 .= " ($id,'$image',0),";
            }
            $SQL3 = substr($SQL3, 0, strlen($SQL3) - 1);
            $this->Db->query($SQL2);
            $this->Db->query($SQL3);
        }
        return true;
    }

    /**
     * 
     * @param type $productData
     */
    public function createProduct($productData = array()) {
        
    }

    /**
     * 
     * @param type $productId
     */
    public function getById($productId) {
        
    }

    /**
     * 
     * @param <string> $limitStr
     * @return <array> list
     */
    public function getProductList($orderby = false, $limitStr = false) {
        !$orderby && $orderby = '`product_start` DESC';
        !$limitStr && $limitStr = '25';
        $SQL = sprintf("SELECT * FROM `vproductinfo` WHERE `delete` = '0' ORDER BY %s LIMIT %s;", $orderby, $limitStr);
        return $this->Db->query($SQL, false);
    }

    /**
     * 获取商品列表
     * @param type $cat
     * @param type $page
     * @param type $orderby
     * @param type $searchKey
     * @return boolean
     */
    public function getProductSlist($cat = false, $page = 0, $limit = 10, $orderby = '`product_start` DESC', $home = false, $searchKey = '') {
        if ($cat) {
            $_SQL_limit = $page * $limit . "," . $limit;
            $orderby = trim(urldecode($orderby));
            if (isset($searchKey) && $searchKey != '') {
                $searchKey = urldecode($searchKey);
                $_SQL = sprintf("SELECT * FROM `vproductinfo` WHERE `product_name` LIKE '%%$searchKey%%' AND `delete` = 0 ORDER BY %s LIMIT %s;", $orderby, $_SQL_limit);
            } else if ($home) {
                $_SQL = sprintf("SELECT * FROM `vproductinfo` WHERE `product_cat` = $cat AND `delete` = 0 AND `show_inhome` = 1 ORDER BY %s LIMIT %s;", $orderby, $_SQL_limit);
            } else {
                $_SQL = sprintf("SELECT * FROM `vproductinfo` WHERE `product_cat` = $cat AND `delete` = 0 ORDER BY %s LIMIT %s;", $orderby, $_SQL_limit);
            }
            # echo $_SQL;
            $productList = $this->Db->query($_SQL);
            if (count($productList) == 0) {
                return false;
            } else {
                return $this->Db->query($_SQL);
            }
        } else {
            return false;
        }
    }

    /**
     * 获取商品分类信息
     * @param type $catid
     * @return <string>
     */
    public function getCatInfo($catid) {
        $catid = addslashes(intval($catid));
        $SQL = "SELECT * FROM `product_category` WHERE `cat_id` = $catid; ORDER BY `cat_order` DESC";
        $res = $this->Db->query($SQL);
        return $res[0];
    }

    /**
     * 获取商品分类列表
     * @param type $limit
     * @return <array>
     */
    public function getCatList() {
        $SQL = "SELECT * FROM `product_category` ORDER BY cat_order ASC;";
        return $this->Db->query($SQL);
    }

    /**
     * 删除商品
     * @param type $id
     * @return type
     */
    public function deleteProduct($id) {
        return $this->Db->query("UPDATE `products_info` SET `delete` = 1 WHERE `product_id` = $id;");
    }

}
